import { URL } from '@/env.js'

const unLoginCodes = [2]

function useRequest(params) {
	return new Promise((resolve) => {
		const url = `${URL}${params.url}`
		uni.request({
			url,
			method: params.method || 'POST',
			header: {
				Authorization: uni.getStorageSync('token') || undefined,
			},
			data: params.data,
			success: result => {
				const res = result.data
				console.log(uni.getStorageSync('token'))
				console.log(url)
				console.log(params.data)
				console.log(res)
				if(typeof(res) == 'string') {
					console.log('报错了')
					console.log(res)
					return
				}
				if(unLoginCodes.includes(res.code)) {
					uni.toast(res.message)
					// store.commit('user/setUserToken','')
					uni.reLaunch({
						url: '/pages/login/index'
					})
					return
				}
				resolve(res)
			}
		})
	})
}

export {
	useRequest,
}